//
//  MKReaderCfg.swift
//  Fanmugua
//
//  Created by MK on 2021/3/27.
//  Copyright © 2021 taolang. All rights reserved.
//

import UIKit

/// 单利对象
private var configure:MKReaderCfg?

class MKReaderCfg: NSObject {
    
//    @objc var isNightStyle:NSNumber!    //是否夜晚模式 0:白天  1:夜晚
    @objc var effectIndex:NSNumber!     //翻页类型索引
    @objc var bgColorIndex:NSNumber!    //背景颜色索引
    @objc var spacingIndex:NSNumber!    //间距类型索引 - 行间距
    
    
    @objc var fontIndex:NSNumber!       //字体类型索引
    
    @objc var fontSize:NSNumber!        //字体大小
    @objc var characterSpacingSize:NSNumber!    //间距类型索引 - 行间距
    @objc var lineSpacing:NSNumber!    //行间距
    @objc var paragraphSpacing:NSNumber!    //段间距
    
    
    
    
//    @objc var progressIndex:NSNumber!   /// 进度显示索引
//    var openLongPress:Bool = false    //开启长按菜单功能 (滚动模式是不支持长按功能的)

    //单例
    @objc class func shared() -> MKReaderCfg {
        if configure == nil {
            configure = MKReaderCfg(MKUserDefaults.object(MKREADER_KEY_CONFIGURE))
        }
        return configure!
    }
    
    class func model(_ dict:Any?) -> MKReaderCfg {
        return MKReaderCfg(dict)
    }

    init(_ dict:Any? = nil) {
        super.init()
        if dict != nil {
            setValuesForKeys(dict as! [String : Any])
        }
        initData()
    }
    
    private func initData(){
//        if (isNightStyle == nil){
//            isNightStyle = NSNumber(value: 0)
//        }
        
        if (effectIndex == nil || MKReaderEffectType(rawValue: effectIndex.intValue) == nil){
            effectIndex = NSNumber(value: MKReaderEffectType.simulation.rawValue)
        }
        
        if (bgColorIndex == nil || bgColorIndex.intValue >= MKREADER_BG_COLORS.count) {
            bgColorIndex = NSNumber(value: 0)
        }
        
//        if (spacingIndex == nil || MKReaderSpacingType(rawValue: spacingIndex.intValue) == nil){
//            spacingIndex = NSNumber(value: MKReaderSpacingType.big.rawValue)
//        }
        
        if (lineSpacing == nil || lineSpacing.intValue > MKREADER_TEXT_LINE_SPACE_MAX || lineSpacing.intValue < MKREADER_TEXT_LINE_SPACE_MIN){
            lineSpacing = NSNumber(value: MKREADER_TEXT_LINE_SPACE_DEFAULT)
        }
        
        if (characterSpacingSize == nil || characterSpacingSize.intValue > MKREADER_TEXT_CHARACTER_SPACE_MAX || characterSpacingSize.intValue < MKREADER_TEXT_CHARACTER_SPACE_MIN){
            characterSpacingSize = NSNumber(value: MKREADER_TEXT_CHARACTER_SPACE_DEFAULT)
        }
        
        if (paragraphSpacing == nil || paragraphSpacing.intValue > MKREADER_TEXT_PARAGRAPH_SPACE_MAX || paragraphSpacing.intValue < MKREADER_TEXT_PARAGRAPH_SPACE_MIN){
            paragraphSpacing = NSNumber(value: MKREADER_TEXT_PARAGRAPH_SPACE_DEFAULT)
        }
        
//        if (progressIndex == nil || MKReaderProgressType(rawValue: progressIndex.intValue) == nil) {
//            progressIndex = NSNumber(value: MKReaderProgressType.page.rawValue)
//        }
//
        if (fontIndex == nil || MKReaderFontType(rawValue: fontIndex.intValue) == nil) {
            fontIndex = NSNumber(value: MKReaderFontType.system.rawValue)
        }

        if (fontSize == nil || fontSize.intValue > MKREADER_FONT_SIZE_MAX || fontSize.intValue < MKREADER_FONT_SIZE_MIN){
            fontSize = NSNumber(value: MKREADER_FONT_SIZE_DEFAULT)
        }
    }
    
    var effectType:MKReaderEffectType! {
        return MKReaderEffectType(rawValue: effectIndex.intValue)
    }
    
//    /** 快捷获取
//     * 使用分页进度 || 总文章进度(网络文章也可以使用)
//     * 总文章进度注意: 总文章进度需要有整本书的章节总数,以及当前章节带有从0开始排序的索引。
//     * 如果还需要在拖拽底部功能条上进度条过程中展示章节名,则需要带上章节列表数据,并去 DZMRMProgressView 文件中找到 ASValueTrackingSliderDataSource 修改返回数据源为章节名。
//     */
    
//    //进度类型
//    var progressType:MKReaderProgressType! {
//        return MKReaderProgressType(rawValue: progressIndex.intValue)
//    }

    //保持设置
    @objc func saveConfigure(){
        let dict = [
//            "isNightStyle":isNightStyle,
            "effectIndex": effectIndex,
            "bgColorIndex": bgColorIndex,
//            "spacingIndex": spacingIndex,
            "lineSpacing": lineSpacing,
            "characterSpacingSize": characterSpacingSize,
            "paragraphSpacing": paragraphSpacing,
            "fontIndex": fontIndex,
//            "progressIndex": progressIndex,
            "fontSize": fontSize
        ]
        MKUserDefaults.setObject(dict, MKREADER_KEY_CONFIGURE)
    }

    override func setValue(_ value: Any?, forUndefinedKey key: String) { }
    
    
    
    
    //背景颜色
    @objc var bgColor:UIColor! {
        return MKREADER_BG_COLORS[bgColorIndex.intValue]
    }
    
    //字色
    @objc var textColor:UIColor! {
        return MKREADER_TEXT_COLORS[bgColorIndex.intValue]
    }
    
    //状态栏文字颜色
    @objc var statusTextColor:UIColor! {
        return MKREADER_TEXT_COLORS[bgColorIndex.intValue]
    }
    
    //上下 菜单背景色
    @objc var menuBgColor:UIColor! {
        return RGBA(38, 38, 38);
    }
    
    @objc var menuTextColor:UIColor! {
        return RGBA(132, 132, 132);
    }
    
    var inputFieldBgColor:UIColor! {
        return RGBA(126, 126, 126);
    }
    
    var selectedColor:UIColor! {
        return RGBA(255, 117, 54);
    }
    
    var unSelectedColor:UIColor! {
        return RGBA(112, 112, 112)
    }
    
//    var imageStyle:String! {
//        return String(bgColorIndex.intValue + 1)
//    }
  
    //字体
    var fontType:MKReaderFontType! {
        return MKReaderFontType(rawValue: fontIndex.intValue)
    }
    //间距类型
    var spacingType:MKReaderSpacingType! {
        return MKReaderSpacingType(rawValue: spacingIndex.intValue)
    }
    //行间距
//    var lineSpacing:CGFloat! {
//        if spacingType == .big {
//            return MKREADER_TEXT_SPACE_BIG
//        }else if (spacingType == .middle){
//            return MKREADER_TEXT_SPACE_MIDDLE
//        }else{
//            return MKREADER_TEXT_SPACE_SMALL
//        }
//    }
    //段落间距
//    var paragraphSpacing:CGFloat {
//        if spacingType == .big {
//            return MKREADER_PARAGRAPH_SPACE_BIG
//        }else if spacingType == .middle {
//            return MKREADER_PARAGRAPH_SPACE_MIDDLE
//        }else{
//            return MKREADER_PARAGRAPH_SPACE_SMALL
//        }
//    }
    
    // 字体属性
    // isPaging: 为YES的时候只需要返回跟分页相关的属性即可 (原因:包含UIColor,小数点相关的...不可返回,因为无法进行比较)
    @objc func attributes(isTitle:Bool, isPaging:Bool = false) -> [NSAttributedString.Key:Any] {
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineHeightMultiple = 1.0
        if isTitle {
            paragraphStyle.lineSpacing = 0      //行间距
            paragraphStyle.paragraphSpacing = 0 //段间距
            paragraphStyle.alignment = .center  //对齐
        }else{
            paragraphStyle.lineSpacing = MKREADER_TEXT_SPACE_BIG
            paragraphStyle.paragraphSpacing = MKREADER_PARAGRAPH_SPACE_BIG
            paragraphStyle.alignment = .justified
            paragraphStyle.firstLineHeadIndent = font().pointSize * 2;
        }
        if isPaging {
            return [.font: font(isTitle: isTitle), .paragraphStyle: paragraphStyle]
        }else{
            return [.foregroundColor: textColor as Any, .font: font(isTitle: isTitle), .paragraphStyle:paragraphStyle]
        }
    }

    //阅读字体
    func font(isTitle:Bool = false) -> UIFont {
        let size = SA_SIZE(CGFloat(fontSize.intValue + (isTitle ? MKREADER_TITLE_FONT_SIZE_SPACE : 0)))
        if fontType == .one {
            return UIFont(name: "EuphemiaUCAS-Italic", size: size)!
        }else if fontType == .two {
            return UIFont(name: "AmericanTypewriter-Light", size: size)!
        }else if fontType == .three {
            return UIFont(name: "Papyrus", size: size)!
        }else{
            return UIFont.systemFont(ofSize: size)
        }
    }

}
